package org.liveboardgames.agricola.control.delegates
{
    import com.adobe.cairngorm.business.ServiceLocator;
    import com.adobe.cairngorm.control.CairngormEventDispatcher;
    
    import mx.rpc.IResponder;
    
    import org.liveboardgames.agricola.control.events.AddResourceEvent;
    import org.liveboardgames.agricola.control.events.HarvestStepCompleteEvent;
    import org.liveboardgames.agricola.control.events.PlayMultipleCardEvent;
    import org.liveboardgames.agricola.model.AgricolaModelLocator;
    import org.liveboardgames.agricola.model.enums.HarvestStepEnum;
    import org.liveboardgames.agricola.model.enums.ResourceTypeEnum;
    import org.liveboardgames.agricola.vo.card.BeggingCard;
    import org.liveboardgames.agricola.vo.common.IResourceContainer;
    import org.liveboardgames.agricola.vo.player.Player;
    
    public class HarvestFeedDelegate
    {
        private var _responder:IResponder;
        
        private var _service:Object;
        
        public function HarvestFeedDelegate(responder:IResponder)
        {
            this._service = ServiceLocator.getInstance().getRemoteObject( "harvestService" );
            this._responder = responder;
        }
        
        public function feed(player:Player, foodNeeded:int, foodGiven:int):void
        {
        	var call:Object = _service.harvestFeed(AgricolaModelLocator.getInstance().game.id, player.id, foodNeeded, foodGiven);
			call.resultHandler = _responder.result;
			call.faultHandler = _responder.fault;
//            setTimeout(feedService, 0, player, foodNeeded, foodGiven);
        }
        
        private function feedService(player:Player, foodNeeded:int, foodGiven:int):void
        {
            // Add applicable begging cards
            var numberOfBeggingCards:int = foodNeeded - foodGiven;
            if (numberOfBeggingCards > 0)
            {
                var beggingCard:BeggingCard = new BeggingCard();
                var playCardEvent:PlayMultipleCardEvent = new PlayMultipleCardEvent(PlayMultipleCardEvent.PLAY_MULTIPLE_CARD);
                playCardEvent.player = player;
                playCardEvent.quantity = numberOfBeggingCards;
                playCardEvent.card = beggingCard;
                CairngormEventDispatcher.getInstance().dispatchEvent(playCardEvent);
            }
            
            // If too much food was given, give the excess back to the player
            var excessFood:int = -numberOfBeggingCards;
            if (excessFood > 0)
            {
                var addResourceEvent:AddResourceEvent = new AddResourceEvent(AddResourceEvent.ADD_RESOURCE);
                addResourceEvent.resourceHost = player.farm as IResourceContainer;
                addResourceEvent.resources = addResourceEvent.getResourceAsMap(ResourceTypeEnum.FOOD, excessFood);
                CairngormEventDispatcher.getInstance().dispatchEvent(addResourceEvent);
            }
            
            // Create an event to say the feed step of the harvest is done
            var harvestStepCompleteEvent:HarvestStepCompleteEvent = new HarvestStepCompleteEvent(HarvestStepCompleteEvent.
                HARVEST_STEP_COMPLETE);
            harvestStepCompleteEvent.step = HarvestStepEnum.FEED;
            harvestStepCompleteEvent.player = player;
            CairngormEventDispatcher.getInstance().dispatchEvent(harvestStepCompleteEvent);
        }
    }
}